<?php namespace Phpcmf\Controllers;

class Dashangpinglun extends \Phpcmf\Home\Module {


    public function index() {

        //$this->_module_init(APP_DIR);

        $id = (int)\Phpcmf\Service::L('Input')->get('id');
		
        (!$id) && exit($this->_msg(0, dr_lang('ID不能为空')));

        !$this->uid && exit($this->_msg(0, '你还没有登录'));

        $data = \Phpcmf\Service::M()->table('1_quan_form_son_comment')->get($id);
		
        !$data && $this->_msg(0, dr_lang('内容【id#%s】不存在',  $id));
        //!isset($data['jifenf']) && $this->_msg(0, dr_lang('此模块没有找到jifenf字段'));
		//$score = intval($data['money']);
		//(!$score2) && exit($this->_msg(0, dr_lang('赏金没有选择')));
		$score = \Phpcmf\Service::L('Input')->get('money2');
		
		//$score2 = \Phpcmf\Service::L('Input')->post('money2');
		if(!$score) {
			$score = \Phpcmf\Service::L('Input')->get('money');
			(!$score) && exit($this->_msg(0, dr_lang('打赏金额没有选择')));
			//$this->_msg(0, dr_lang('请选择赏金'));
		}
		// 全站
		$dashangdata = \Phpcmf\Service::M()->table('1_quan_shezhi')->where('dirname', 'dashang')->getRow();
		$dashangdatas = dr_string2array($dashangdata['setting']);
		$moneydata = $dashangdatas['pages'];
		//dr_debug('文件名称$jifendata', $jifendata);
		if($moneydata['quan_dashang_xianzhi_rmb'] > $score && $moneydata['quan_dashang_xianzhi_rmb'] > 0){
			$this->_msg(0, dr_lang('打赏金额不能小于'.$moneydata['quan_dashang_xianzhi_rmb'].'元'));
		}
		$id = (int)\Phpcmf\Service::L('input')->get('id');
		$table = '1_quan_dizhi';
		$sku = \Phpcmf\Service::M()->db->table($table)->where('uid', $this->uid)->where('moren',1)->get()->getRowArray();
		//$skus = dr_string2array($sku);
		
		
		\Phpcmf\Service::V()->display('mform_show-dashang-pinglun-buy.html');exit;



    }
	
	public function zhifu() {
		$id = (int)\Phpcmf\Service::L('input')->get('id');
		$pay = (int)\Phpcmf\Service::L('Input')->get('pay');
		
		//$skus = dr_string2array($sku);
		$data =dr_payform("my-quan_dashangpinglun-".$id."-1",);
		
		$rt=[
			'code'=>1,
		    'msg'=>'支付页面x',
			'data'=>$data,
		];
		
		return $this->_json($rt['code'], $rt['msg'], $rt['data']);
	}
	
	public function jifen() {
		$id = (int)\Phpcmf\Service::L('input')->get('id');
		if(IS_POST){
			$money = \Phpcmf\Service::L('input')->post('money');
			// 全站
			$dashangdata = \Phpcmf\Service::M()->table('1_quan_shezhi')->where('dirname', 'dashang')->getRow();
			$dashangdatas = dr_string2array($dashangdata['setting']);
			$jifendata = $dashangdatas['pages'];
			//dr_debug('文件名称$jifendata', $jifendata);
			if($jifendata['quan_dashang_xianzhi_jifen'] > $money && $moneydata['quan_dashang_xianzhi_jifen'] > 0){
				$this->_msg(0, dr_lang(SITE_SCORE.'不能小于'.$jifendata['quan_dashang_xianzhi_jifen']));
			}
			//dr_debug('文件名称$money', $money);
			$data = \Phpcmf\Service::M()->db->table('1_quan_form_son')->where('id', $id)->get()->getRowArray();
			if($data['title']){
				$biaoti = '<a href="/index.php?s=quan&c=son&id='.$id.'">'.$data['title'].'</a>';
			} else {
				$biaoti = '<a href="/index.php?s=quan&c=son&id='.$id.'">'.dr_strcut(dr_clearhtml($data['content']), 30).'</a>';
			}
			//作者uid
			$userweixins=\Phpcmf\Service::M()->db->table('member_oauth')->where('uid', $data['uid'])->get()->getRowArray();
			$users=dr_member_info($data['uid']);
			if ($userweixins['nickname']){
				$usermc = $userweixins['nickname'];
			} elseif ($users['name']) {
				$usermc = '<a href="/index.php?s=hy&c=show&uid='.$data['uid'].'">'.$users['name'].'</a>';
			} else {
				$usermc = '<a href="/index.php?s=hy&c=show&uid='.$data['uid'].'">'.$users['username'].'</a>';
			}
			//打赏uid
			$userweixinda=\Phpcmf\Service::M()->db->table('member_oauth')->where('uid', $this->uid)->get()->getRowArray();
			$userda=dr_member_info($this->uid);
			if ($userweixinda['nickname']){
				$userda = $userweixinda['nickname'];
			} elseif ($userda['name']) {
				$userda = '<a href="/index.php?s=hy&c=show&uid='.$this->uid.'">'.$userda['name'].'</a>';
			} else {
				$userda = '<a href="/index.php?s=hy&c=show&uid='.$this->uid.'">'.$userda['username'].'</a>';
			}
			if ((float)$this->member['score'] <= 0 ) {
			    return $this->_msg(0, dr_lang('账户'.SITE_SCORE.'不足'));
			} elseif ($this->member['score'] - $money < 0) {
			    return $this->_msg(0, dr_lang('账户可用'.SITE_SCORE.'不足'));
			}
			if(!$money){
				return $this->_msg(0, dr_lang(SITE_SCORE.'不能为空'));
			}
			// 打赏记录
			\Phpcmf\Service::M()->db->table('1_quan_dashang_buy')->insert([
				'quanid' => $data['cid'],
			    'tid' => $id,
			    'uid' => $this->uid,
				'touid' => $data['uid'],
				'feiyong' => '',
				'jifen' => $money,
			    'leixing' => 2,
			    'inputtime' => SYS_TIME
			]);
			// 扣积分
			\Phpcmf\Service::M('member')->add_score($this->uid, -$money, '您向 ' .$usermc. ' 的《'.$biaoti.'》打赏'.$money.''.SITE_SCORE);
			    
			// 打积分到作者
			\Phpcmf\Service::M('member')->add_score($data['uid'], $money, $userda.' 向您的 《'.$biaoti.'》打赏'.$money.''.SITE_SCORE);
			$url = "index.php?s=quan&c=son&id=".$id;
			//return dr_url_prefix($url); // 跳转
			$this->_msg(1, dr_lang('打赏成功'), dr_url_prefix($url));
			
		}
		/*$yidashang = \Phpcmf\Service::M()->table(SITE_ID.'_quan_dashang_buy')->where('id', $id)->where('uid', $this->uid)->getRow();
		if($yidashang){
			$url = "index.php?s=quan&c=show&id=".$id;
			return dr_url_prefix($url); // 跳转
		}*/
		
		
		\Phpcmf\Service::V()->display('mform_show-dashang-buy-jifen.html');exit;
	}
	public function jifenzhifu() {
		$id = (int)\Phpcmf\Service::L('input')->get('id');
		$pay = (int)\Phpcmf\Service::L('Input')->get('pay');
		
		//$skus = dr_string2array($sku);
		$data =dr_payform("my-quan_dashangpinglun-".$id."-1",);
		
		$rt=[
			'code'=>1,
		    'msg'=>'支付页面',
			'data'=>$data,
		];
		
		return $this->_json($rt['code'], $rt['msg'], $rt['data']);
	}
	
	public function list() {
		$tid = (int)\Phpcmf\Service::L('input')->get('tid');
		\Phpcmf\Service::V()->assign(\Phpcmf\Service::L('Seo')->get_seo_value($data, [
		    'meta_title' => '打赏',
		    'meta_keywords' => isset($this->form['setting']['seo']['keywords']) && $this->form['setting']['seo']['keywords'] ? $this->form['setting']['seo']['keywords'] : $data['title'].SITE_SEOJOIN.dr_lang($this->form['name']),
		    'meta_description' => isset($this->form['setting']['seo']['description']) && $this->form['setting']['seo']['description'] ? $this->form['setting']['seo']['description'] : $data['title'].SITE_SEOJOIN.dr_lang($this->form['name']),
		]));
		\Phpcmf\Service::V()->assign([
		    'data' => $data,
		]);
		\Phpcmf\Service::V()->display("mform_show-dashang-list.html");
	}
}
